package com.happy_lab_management.demo.system.controller;

import com.happy_lab_management.demo.admin.semester.service.SemesterService;
import com.happy_lab_management.demo.system.service.SystemService;
import com.happy_lab_management.entity.Semester;
import com.happy_lab_management.entity.System;
import com.happy_lab_management.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private SystemService systemService;
    @Autowired
    private SemesterService semesterService;
    @RequestMapping("/list")
    public Result<List<System>> list(){
        List<System> systems = systemService.list();
        return Result.success(systems);
    }
    @RequestMapping("/getCurrentSe")
    public Result<Semester> getCurrentSe(){
        Integer currentSemesterId = systemService.getValueByKey("current_semester_id");
        Semester byId = semesterService.getById(currentSemesterId);
        return Result.success(byId);
    }
//edit设置当前学期
    @RequestMapping("/edit")
    public Result<String> edit(@RequestParam(name = "id") Integer id){
        if(id==null) return Result.error("没有选择当前学期");
        try{
            boolean b = systemService.setValueByKey("current_semester_id",id);
            if(!b){
                return Result.error("设置当前学期失败");
            }
        }catch (Exception e ){
            return Result.error("设置当前学期失败");
        }
        return Result.success("设置当前学期成功");
    }

    @RequestMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id") Integer id){
        if(id==null) return Result.error("没有选择学期");
        try{
            boolean b = semesterService.removeById(id);
            if(!b){
                return Result.error("删除学期失败");
            }
        }catch (Exception e ){
            return Result.error("删除学期失败");
        }
        return Result.success("删除学期成功");
    }

}
